package testConsoleIO;

import junit.framework.*;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;



import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

import consoleIO.ConsoleIO;



public class ConsoleIOTest extends TestCase 
{

	private ConsoleIO consoleIO;
	
	public ConsoleIOTest(String name)
	{
		super(name);
	}
	
	
	@Before
	public void setUp() throws Exception {
		
		consoleIO = new ConsoleIO(); 
		
	}

	@After
	public void tearDown() throws Exception {}

	
	@Test
	public void testGetInfoWord() 
	{
		String input="Word";
		String expected="Word";
		InputStream  in = new ByteArrayInputStream(input.getBytes());
		System.setIn(in);
		
		String actual=consoleIO.getInfo();
		
		assertEquals(expected, actual);
		
	}
	
	@Test
	public void testGetInfoSentence() 
	{
		String input="This is a sentence";
		String expected="This";
		InputStream  in = new ByteArrayInputStream(input.getBytes());
		System.setIn(in);
				
		String actual=consoleIO.getInfo();		
		
		assertEquals(expected, actual);
		

	}
	
	@Test
	public void testGetInfoALLInput2012() 
	{
		String acadYr="2012";
		String sem="A";
		String form="5";
		String chinRptRm="315";
		String chinExamRm="302 303 304 305 306 307 308 -1";
		String chinDate="20/01/2013";
		String chinTime="09:00";
		String chinDuration="25";
		String engRptRm="415";
		String engExamRm="402 403 404 405 406 407 408 -1";
		String engDate="20/01/2013";
		String engTime="09:00";
		String engDuration="20";
		String scheduleMode="1";
		
		String input=acadYr+" "+sem+" "+form+" "+chinRptRm+" "+chinExamRm+" "+chinDate+" "+chinTime+" "+chinDuration+" "+engRptRm+" "+engExamRm+" "+engDate+" "+engTime+" "+engDuration+" "+scheduleMode; 
		
		String expected="2012 A 5 315 7 302 303 304 305 306 307 308 20/01/2013 09:00 25 415 7 402 403 404 405 406 407 408 20/01/2013 09:00 20 1";
		
		InputStream  in = new ByteArrayInputStream(input.getBytes());
		System.setIn(in);
			
		String actual=consoleIO.getInfo();	
		assertEquals(expected, actual);
		
	}
	
	@Test
	public void testGetInfoALLInput2013() 
	{
		String acadYr="2013";
		String sem="A";
		String form="5";
		String chinRptRm="315";
		String chinExamRm="302 303 304 305 306 307 308 -1";
		String chinDate="20/01/2013";
		String chinTime="09:00";
		String chinDuration="25";
		String engRptRm="415";
		String engExamRm="402 403 404 405 406 407 -1";
		String engDate="20/01/2013";
		String engTime="09:00";
		String engDuration="20";
		String scheduleMode="1";
		
		String input=acadYr+" "+sem+" "+form+" "+chinRptRm+" "+chinExamRm+" "+chinDate+" "+chinTime+" "+chinDuration+" "+engRptRm+" "+engExamRm+" "+engDate+" "+engTime+" "+engDuration+" "+scheduleMode; 
		
		String expected="2012 A 5 315 7 302 303 304 305 306 307 308 20/01/2013 09:00 25 415 6 402 403 404 405 406 407 20/01/2013 09:00 20 1";
		
		InputStream  in = new ByteArrayInputStream(input.getBytes());
		System.setIn(in);
		
		
	
		String actual=consoleIO.getInfo();	
		assertEquals(expected, actual);
		
	}

	@Test
	public void testShowInfoALLInputInOneLine2012() throws IOException 
	{
		String acadYr="2012";
		String sem="A";
		String form="5";
		String chinRptRm="315";
		String chinExamRm="302 303 304 305 306 307 308 -1";
		String chinDate="20/01/2013";
		String chinTime="09:00";
		String chinDuration="25";
		String engRptRm="415";
		String engExamRm="402 403 404 405 406 407 408 -1";
		String engDate="20/01/2013";
		String engTime="09:00";
		String engDuration="20";
		String scheduleMode="1";
		
		String input=acadYr+" "+sem+" "+form+" "+chinRptRm+" "+chinExamRm+" "+chinDate+" "+chinTime+" "+chinDuration+" "+engRptRm+" "+engExamRm+" "+engDate+" "+engTime+" "+engDuration+" "+scheduleMode; 
		
		String expected="showInfo: 2012 A 5 315 7 302 303 304 305 306 307 308 20/01/2013 09:00 25 415 6 402 403 404 405 406 407 20/01/2013 09:00 20 1";
		
		//consoleIO.getInfo();
		
		
		
		InputStream  in = new ByteArrayInputStream(input.getBytes());
		System.setIn(in);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
     
        consoleIO.showInfo(input);
        
        
        out.flush();
         
        String whatWasPrinted = new String(out.toByteArray());
        String[] linesOfOutput = whatWasPrinted.split(System.getProperty("line.separator"));
        
        
        
        assertEquals(linesOfOutput.length, 1);
        assertEquals(expected, linesOfOutput[0]);
		
	}
	@Test
	public void testShowInfoALLInputInMultiLineFake2012() throws IOException 
	{
		String acadYr="2012";
		String sem="A";
		String form="5";
		String chinRptRm="315";
		String chinExamRm="302 303 304 305 306 307 308 -1";
		String chinDate="20/01/2013";
		String chinTime="09:00";
		String chinDuration="25";
		String engRptRm="415";
		String engExamRm="402 403 404 405 406 407 408 -1";
		String engDate="20/01/2013";
		String engTime="09:00";
		String engDuration="20";
		String scheduleMode="1";
		
		String input=acadYr+" "+sem+" "+form+" "+chinRptRm+" "+chinExamRm+" "+chinDate+" "+chinTime+" "+chinDuration+" "+engRptRm+" "+engExamRm+" "+engDate+" "+engTime+" "+engDuration+" "+scheduleMode; 
		
		//String expected="showInfo: 2012 A 5 315 7 302 303 304 305 306 307 308 20/01/2013 09:00 25 415 6 402 403 404 405 406 407 20/01/2013 09:00 20 1";
		
		
		String expectedLine1="showInfo: 2012 A 5 315 302 303 304 305 306 307 308 -1 20/01/2013 09:00 25 415 402 403 404 405 406 407 408 -1 20/01/2013 09:00 20 1";
		String expectedLine2="showInfo: 2012 A 5 315 302 303 304 305 306 307 308 -1 20/01/2013 09:00 25 415 402 403 404 405 406 407 408 -1 20/01/2013 09:00 20 1";
		String expectedLine3="showInfo: 2012 A 5 315 302 303 304 305 306 307 308 -1 20/01/2013 09:00 25 415 402 403 404 405 406 407 408 -1 20/01/2013 09:00 20 1";
		//consoleIO.getInfo();
		
	
		
		InputStream  in = new ByteArrayInputStream(input.getBytes());
		System.setIn(in);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
     
        consoleIO.showInfo(input);
        
        
        out.flush();
         
        String whatWasPrinted = new String(out.toByteArray());
        String[] linesOfOutput = whatWasPrinted.split(System.getProperty("line.separator"));
        
        
        
        assertEquals(linesOfOutput.length, 3);
   
        assertEquals(expectedLine1, linesOfOutput[0]);
        assertEquals(expectedLine2, linesOfOutput[1]);
        assertEquals(expectedLine3, linesOfOutput[2]);
	}
	
	@Test
	public void testShowInfoALLInputInMultiLine2012() throws IOException 
	{
		String acadYr="2012";
		String sem="A";
		String form="5";
		String chinRptRm="315";
		String chinExamRm="302 303 304 305 306 307 308";
		String chinDate="20/01/2013";
		String chinTime="09:00";
		String chinDuration="25";
		String engRptRm="415";
		String engExamRm="402 403 404 405 406 407 408";
		String engDate="20/01/2013";
		String engTime="09:00";
		String engDuration="20";
		String scheduleMode="1";
		
		String input=acadYr+" "+sem+" "+form+" "+chinRptRm+" "+chinExamRm+" "+chinDate+" "+chinTime+" "+chinDuration+" "+engRptRm+" "+engExamRm+" "+engDate+" "+engTime+" "+engDuration+" "+scheduleMode; 
		
		//String expected="showInfo: 2012 A 5 315 7 302 303 304 305 306 307 308 20/01/2013 09:00 25 415 6 402 403 404 405 406 407 20/01/2013 09:00 20 1";
		
		String[] expectedLine = new String[17];
		expectedLine[0]="Academic Year: "+acadYr;
		expectedLine[1]="Semester: "+sem;
		expectedLine[2]="Form: "+form;
		expectedLine[3]="";
		expectedLine[4]="CHINESE ORAL EXAM INFO:";
		expectedLine[5]="Reporting Room: "+chinRptRm;
		expectedLine[6]="No. of Exam Rooms: 7 ("+chinExamRm+")";
		expectedLine[7]="Date: "+chinDate;
		expectedLine[8]="Starting Time: "+chinDuration;
		expectedLine[9]="Duration: "+chinDuration+"min per session";
		expectedLine[10]="";
		expectedLine[11]="ENGLISH ORAL EXAM INFO:";
		expectedLine[12]="Reporting Room: "+engRptRm;
		expectedLine[13]="No. of Exam Rooms: 7 ("+engExamRm+")";
		expectedLine[14]="Date: "+engDate;
		expectedLine[15]="Starting Time: "+engDuration;
		expectedLine[16]="Duration: "+engDuration+"min per session";
		//consoleIO.getInfo();
		
	
		
		InputStream  in = new ByteArrayInputStream(input.getBytes());
		System.setIn(in);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
     
        consoleIO.showInfo(input);
        
        
        out.flush();
         
        String whatWasPrinted = new String(out.toByteArray());
        String[] linesOfOutput = whatWasPrinted.split(System.getProperty("line.separator"));
        
        
        
        //assertEquals(linesOfOutput.length, expectedLine.length);
        int length=expectedLine.length;
        for (int i=0; i<length; i++) {
        	assertEquals(expectedLine[i], linesOfOutput[i]);
        }
       
	}
	
	

	@Test
	public void testShowSchedule() 
	{
		fail("Not yet implemented");
	}

}
